package br.com.salao.session;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;

import br.com.salao.ejb.dao.ServicosDao;
import br.com.salao.ejb.dao.impl.ServicosDaoImpl;
import br.com.salao.entity.Servicos;

@Stateless
@Remote(ServicosBase.class)
public class ServicosBean implements ServicosBase {

	/**
	 * @author Dri
	 */
	private static final long serialVersionUID = 1L;

	@PersistenceContext(unitName = "SalaoBeleza")
	private EntityManager em;

	private Logger logger = Logger.getLogger(ServicosBean.class.getName());

	public ServicosBean(){
		//em = JpaUtil.getEntityManager();
	}

	@TransactionAttribute
	public boolean incluir(Servicos servicos, String horaMinuto) throws ParseException{

		Date time;
		DateFormat formata = new SimpleDateFormat("HH:mm");
		time = (Date)formata.parse(horaMinuto);
		System.out.println(time.toString());

		servicos.setDuracaoServico(time);

		//Caso esteja habilitado o WS			

		ServicosDao dao = new ServicosDaoImpl(em);
		try{
			dao.inserir(servicos);
			return true;
		}catch (Exception e) {

			StringBuffer sb = new StringBuffer();
			sb.append(e.getStackTrace() + "\n");
			sb.append(e.getCause() + "\n");
			sb.append(e.getMessage() + "\n");

			logger.error(sb.toString());

			e.printStackTrace();

			return false;
		}
	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<Servicos> listar(){	
		ServicosDao dao = new ServicosDaoImpl(em);
		return dao.listar();
	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public Servicos buscarServicoPorId(int id){	
		ServicosDao dao = new ServicosDaoImpl(em);
		return dao.buscarServicoPorId(id);
	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<Servicos> buscarServicoPorIdEstabelecimento(int idEstabelecimento){	
		ServicosDao dao = new ServicosDaoImpl(em);
		return dao.buscarServicoPorIdEstabelecimento(idEstabelecimento);
	}

	@TransactionAttribute
	public Servicos inserir(Servicos servicos) {
		ServicosDao dao = new ServicosDaoImpl(em);
		return dao.inserir(servicos);
	}


}
